package com.timoon.sample.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.timoon.component.panel.jqgrid.JqGridHelper;
import com.timoon.component.panel.jqgrid.JqGridParameter;
import com.timoon.sample.dao.ProductDAO;
import com.timoon.sample.dao.model.Product;
import com.timoon.sample.dao.model.ProductExample;
import com.timoon.webcomponent.utility.product.girdpic.GridBigPicService;

/**
 *<p></p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013.4.1</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */

@Service(value="com.timoon.sample.service.GridBigPicServiceImpl")
public class GridBigPicServiceImpl implements GridBigPicService {

	
	@Resource
	private ProductDAO productDAOImpl;
	
	@Override
	public List<Product> getPruductsByType(final Integer type) {
		// TODO Auto-generated method stub
		return this.productDAOImpl.selectByExample(new ProductExample(){
			{
				this.createCriteria().andStatusNotEqualTo(0).andTypeEqualTo(type);
			}
		});
	}

	public ProductDAO getProductDAOImpl() {
		return productDAOImpl;
	}

	public void setProductDAOImpl(ProductDAO productDAOImpl) {
		this.productDAOImpl = productDAOImpl;
	}

	@Override
	public List<Product> getPageProductBySortId(Integer sortId, Integer page,
			Integer rows,Integer records) {
		// TODO Auto-generated method stub
		//借助jqgridParameter来获取分页product
		JqGridParameter param=new JqGridParameter();
		JqGridHelper helper=new JqGridHelper();
		
		Map<String,String> paramMap=new HashMap<String,String>();
		paramMap.put("sort_id", sortId.toString());
		
		param.setMap(paramMap);
		
		Integer startIndex=helper.getStartIndex(rows,page,records);
		
		param.setPage(page);
		param.setRows(rows);
		param.setStartIndex(startIndex);
		
		List<Product> products=this.productDAOImpl.pagingPorduct(param);

		return products;
	}
	
	@Override
	public Integer countPageProductBySortId(Integer sortId) {
		// TODO Auto-generated method stub
		
		JqGridParameter param=new JqGridParameter();
		
		Map<String,String> paramMap=new HashMap<String,String>();
		paramMap.put("sort_id", sortId.toString());
		
		param.setMap(paramMap);
		return this.productDAOImpl.countProductForPaging(param);
	}
	
	private String getParameterInMap(Map<String,Object> map,String key,String defaultValue){
		if(map.containsKey(key)){
			return map.get(key).toString();
		}else{
			return defaultValue;
		}
	}

	@Override
	public List<Product> getPageProductsBySearchInfo(String searchInfo,
			Integer page, Integer rows,Integer records) {
		// TODO Auto-generated method stub
		
		//借助jqgridParameter来获取分页product
		JqGridParameter param=new JqGridParameter();
		JqGridHelper helper=new JqGridHelper();
		
		Map<String,String> paramMap=new HashMap<String,String>();
		paramMap.put("name", searchInfo);//模糊查询
		
		param.setMap(paramMap);
		
		Integer startIndex=helper.getStartIndex(rows,page,records);
		
		param.setPage(page);
		param.setRows(rows);
		param.setStartIndex(startIndex);
		
		
		List<Product> products=this.productDAOImpl.pagingPorduct(param);		
		
		return products;
	}

	@Override
	public Integer countPageProductsBySearchInfo(String searchInfo) {
		// TODO Auto-generated method stub
		
		JqGridParameter param=new JqGridParameter();
		
		Map<String,String> paramMap=new HashMap<String,String>();
		paramMap.put("name", searchInfo);//模糊查询
		
		param.setMap(paramMap);
		
		return this.productDAOImpl.countProductForPaging(param);
	}


}
